import os
import json
from typing import Dict, List, Any

# 默认配置
DEFAULT_SETTINGS = {
    "hotkey": "Ctrl+Space",
    "scan_dir": ["D:\\PortableSoft"],
    "cache_dir": "D:\\FuckDesktop",
    "show_non_executables": True,  # 添加新设置，默认显示所有类型文件
    "filter_keywords": ["卸载", "Uninstall"]  # 添加过滤关键词默认值
}


class ConfigManager:
    """配置管理器"""

    def __init__(self):
        self.config_file = os.path.join(os.path.dirname(
            os.path.abspath(__file__)), "config.json")
        self.settings = self.load_settings()

    def load_settings(self) -> Dict[str, Any]:
        """加载设置"""
        try:
            if os.path.exists(self.config_file):
                with open(self.config_file, 'r', encoding='utf-8') as f:
                    return json.load(f)
            else:
                return DEFAULT_SETTINGS.copy()
        except Exception as e:
            print(f"加载配置文件出错: {e}")
            return DEFAULT_SETTINGS.copy()

    def save_settings(self, settings: Dict[str, Any]) -> bool:
        """保存设置"""
        try:
            # 确保存储目录存在
            os.makedirs(os.path.dirname(self.config_file), exist_ok=True)

            with open(self.config_file, 'w', encoding='utf-8') as f:
                json.dump(settings, f, ensure_ascii=False, indent=2)
            return True
        except Exception as e:
            print(f"保存配置文件出错: {e}")
            return False

    def get_setting(self, key: str, default=None) -> Any:
        """获取设置项"""
        return self.settings.get(key, default)

    def set_setting(self, key: str, value: Any) -> None:
        """设置配置项"""
        self.settings[key] = value

    def reset_to_default(self) -> None:
        """重置为默认设置"""
        self.settings = DEFAULT_SETTINGS.copy()

    def commit(self) -> bool:
        """提交更改到配置文件"""
        return self.save_settings(self.settings)

    @property
    def hotkey(self) -> str:
        """获取快捷键"""
        return self.get_setting("hotkey", DEFAULT_SETTINGS["hotkey"])

    @property
    def scan_directories(self) -> List[str]:
        """获取扫描目录列表"""
        return self.get_setting("scan_dir", DEFAULT_SETTINGS["scan_dir"])

    @property
    def cache_directory(self) -> str:
        """获取缓存目录"""
        return self.get_setting("cache_dir", DEFAULT_SETTINGS["cache_dir"])

    @property
    def show_non_executables(self) -> bool:
        """获取是否显示非可执行文件的快捷方式"""
        return self.get_setting("show_non_executables", DEFAULT_SETTINGS["show_non_executables"])

    @property
    def filter_keywords(self) -> List[str]:
        """获取过滤关键词列表"""
        return self.get_setting("filter_keywords", DEFAULT_SETTINGS["filter_keywords"])

    def update_hotkey(self, hotkey: str) -> None:
        """更新快捷键"""
        self.set_setting("hotkey", hotkey)

    def update_scan_directories(self, directories: List[str]) -> None:
        """更新扫描目录"""
        self.set_setting("scan_dir", directories)

    def update_cache_directory(self, directory: str) -> None:
        """更新缓存目录"""
        self.set_setting("cache_dir", directory)

    def update_show_non_executables(self, show: bool) -> None:
        """更新是否显示非可执行文件的快捷方式设置"""
        self.set_setting("show_non_executables", show)

    def update_filter_keywords(self, keywords: List[str]) -> None:
        """更新过滤关键词列表"""
        self.set_setting("filter_keywords", keywords)


# 创建单例实例
config = ConfigManager()

# 导出变量以便兼容旧代码
scan_dir = config.scan_directories
cache_dir = config.cache_directory
